package org.component.scrollbar
{
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * 滚动条组件
	 * @author anjue
	 */
	public class UIScrollBar extends EventDispatcher
	{
		private var _up:SimpleButton;
		private var _down:SimpleButton;
		private var _back:MovieClip;
		private var _block:MovieClip;
		private var _wheelObject:InteractiveObject;
		
		/** 滚动条滚动范围 **/
		private var _rec:Rectangle;
		
		/** 滚动条控制需要显示长度 **/
		private var _showLength:Number;
		
		/** 滚动条控制的总长度 **/
		private var _totalLength:Number;
		
		/** 按上下按钮滚动的大小 **/
		private var _len:uint = 5;
		
		/** 滚动的索引 **/
		private var _index:uint;
		
		
		private var _stage:Stage;
		
		/**
		 * 滚动条组件构造函数
		 * @param btn_up	向上滚动按钮
		 * @param btn_down	向下滚动按钮
		 * @param mc_back	滚动条背景
		 * @param mc_block	滚动条
		 */
		public function UIScrollBar(btn_up:SimpleButton,btn_down:SimpleButton,mc_back:MovieClip,mc_block:MovieClip):void
		{
			_up = btn_up;
			_down = btn_down;
			_back = mc_back;
			_block = mc_block;
			_block.buttonMode = true;
			_block.addEventListener(MouseEvent.MOUSE_DOWN,blockDown);
			_up.addEventListener(MouseEvent.MOUSE_DOWN,upDown);
			_down.addEventListener(MouseEvent.MOUSE_DOWN,downDown);
			_back.addEventListener(MouseEvent.MOUSE_DOWN,backDown);
			
			if(_block.stage)
			{
				_stage = _block.stage;
			}else
			{
				_block.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			}
		}
		
		private function addToStage(e:Event):void
		{
			_block.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			_stage = _block.stage;
		}
		
		public function set height(value:Number):void
		{
			_back.height = value;
			_down.y = value;
		}
		
		/**
		 * 初始化滚动条功能
		 * @param showLen  需要显示的长度
		 * @param totalLen 总长度
		 */
		public function initScrollFunction(showLen:uint,totalLen:uint,$index:uint=0):void
		{
			_showLength = showLen;
			_totalLength = totalLen;
			
			if(_totalLength > _showLength)
			{
				_up.visible = true;
				_down.visible = true;
				_block.visible = true;
				_back.visible = true;
				var per:Number = _showLength/_totalLength;
				if(per<0.2)
				{
					per = 0.2;
				}else if(per > 0.5)
				{
					per = 0.5;
				}
				_block.height = (_back.height-_up.height*2)*per;
				_block.y = $index*Math.ceil(_back.height-_block.height-2*_up.height)/(_totalLength-_showLength)+_up.height;
				_rec = new Rectangle(_block.x,_up.height,0,Math.ceil(_back.height-(_up.height*2 + _block.height)));
			}else
			{
				_up.visible = false;
				_down.visible = false;
				_block.visible = false;
				_back.visible = false;
			}
		}
		
		/**
		 * 按下滚动条
		 * @param e
		 */
		private function blockDown(e:MouseEvent):void
		{
			_block.startDrag(false,_rec);
			_block.removeEventListener(MouseEvent.MOUSE_DOWN,blockDown);
			_stage.addEventListener(MouseEvent.MOUSE_MOVE,stageMoveHandler);
			_stage.addEventListener(MouseEvent.MOUSE_UP,stageUpHandler);
		}
		
		/**
		 * 点击向上按钮
		 * @param e
		 */
		private function upDown(e:MouseEvent):void
		{
//			var _per:uint = (_back.height-_up.height*2-_block.height)/10;
			if(_block.y-_up.height>0)
			{
				if(_block.y-_up.height>_len)
					_block.y -= _len;
				else
					_block.y = _up.height;
			}
			
			caculateIndex();
		}
		
		/**
		 * 点击向下按钮
		 * @param e
		 */
		private function downDown(e:MouseEvent):void
		{
//			var _per:uint = (_back.height-_up.height*2-_block.height)/10;
			if(_block.y<_back.height-_down.height-_block.height)
			{
				if(_block.y+_block.height+_len<_back.height-_down.height)
					_block.y += _len;
				else
					_block.y = _back.height-_down.height - _block.height;
			}
			
			caculateIndex();
		}
		
		/**
		 * 点击滚动条背景
		 * @param e
		 */
		private function backDown(e:MouseEvent):void
		{
			if(!_back.parent) return;
			
			if(_back.parent.mouseY+_block.height/2>_back.height-_down.height)
				_block.y = _back.height-_down.height - _block.height;
			else if(_back.parent.mouseY-_block.height/2 <_up.height)
				_block.y = _up.height;
			else
				_block.y = _back.parent.mouseY-_block.height/2;
				
			caculateIndex();
		}
		
		private function stageMoveHandler(e:MouseEvent):void
		{
			caculateIndex();
		}
		
		private function stageUpHandler(e:MouseEvent):void
		{
			_block.stopDrag();
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE,stageMoveHandler);
			_stage.removeEventListener(MouseEvent.MOUSE_UP,stageUpHandler);
			
			_block.addEventListener(MouseEvent.MOUSE_DOWN,blockDown);
		}
		
		/**
		 * 设置滚轮对象
		 * @param v
		 */
		public function set wheelObject(v:InteractiveObject):void
		{
			_wheelObject = v;
			
			if(_totalLength>_showLength)
				_wheelObject.addEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
		}
		
		/**
		 * 滑动鼠标滚轮
		 * @param e
		 */
		private function wheelHandler(e:MouseEvent):void
		{
			var num:Number = e.delta;
			if(_block.y-num<_up.height)
				_block.y = _up.height;
			else if(_block.y-num>_back.height - _down.height - _block.height)
				_block.y = _back.height-_down.height - _block.height;
			else
				_block.y -= num;
			
			caculateIndex();
		}
		
		private function caculateIndex():void
		{
			var v:int = Math.ceil(_block.y-_up.height)*(_totalLength-_showLength)/Math.ceil(_back.height-_block.height-2*_up.height)
			_index = v>0?v:0;
			dispatchEvent(new UIScrollEvent(UIScrollEvent.SCROLL_MOVE,_index));
		}
		
		/**
		 * 获取当前滚动的索引
		 * @return 
		 */
		public function get index():uint
		{
			return _index;
		}
		
		/**
		 * 清理事件
		 */
		public function destroy():void
		{
			if(_stage)
			{
				_stage.removeEventListener(MouseEvent.MOUSE_MOVE,stageMoveHandler);
				_stage.removeEventListener(MouseEvent.MOUSE_UP,stageUpHandler);
			}
			_up.removeEventListener(MouseEvent.MOUSE_DOWN,upDown);
			_down.removeEventListener(MouseEvent.MOUSE_DOWN,downDown);
			_block.removeEventListener(MouseEvent.MOUSE_DOWN,blockDown);
			_block.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			_back.removeEventListener(MouseEvent.MOUSE_DOWN,backDown);
			
			if(_wheelObject)
				_wheelObject.removeEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
		}
	}
}